package com.yjx.controller;


import com.github.pagehelper.PageInfo;
import com.yjx.service.CarchargeService;
import com.yjx.service.OwnerService;
import com.yjx.service.ParkingService;
import com.yjx.util.JsonObject;
import com.yjx.util.R;
import com.yjx.vo.Carcharge;
import com.yjx.vo.Owner;
import com.yjx.vo.Parking;
import com.yjx.vo.Userinfo;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * zjx
 */
@RequestMapping("/carcharge")
@RestController
public class CarchargeController {
    @Resource
    private CarchargeService carchargeService;
    @Resource
    private ParkingService parkingService;
    @Resource
    private OwnerService ownerService;

    /**
     *
     * @param carcharge 对象，包含缴费状态
     * @param numbers 车位号
     * @param page 页面数
     * @param limit 每页数据限制
     * @return
     */
    @RequestMapping("/queryCarchargeAll")
    public JsonObject queryCarchargeAll(Carcharge carcharge, String numbers,
                                        @RequestParam(defaultValue ="1") Integer page,
                                        @RequestParam(defaultValue ="15") Integer limit){
        //车位号不为空，将车位对象传入
        if(numbers!=null){
        Parking parking = new Parking();
        parking.setNumbers(numbers);
        carcharge.setParking(parking);
    }
        PageInfo<Carcharge> pageInfo=carchargeService.queryCarchargeAll(page,limit,carcharge);
        return new JsonObject(0,"ok",pageInfo.getTotal(),pageInfo.getList());

    }
    @RequestMapping("/queryCarchargeAll2")
    public JsonObject queryCarchargeAll2(Carcharge carcharge, HttpServletRequest request,
                                         @RequestParam(defaultValue ="1") Integer page,
                                         @RequestParam(defaultValue ="15") Integer limit){
        //从作用域中拿到user对象
        Userinfo userinfo = (Userinfo) request.getSession().getAttribute("user");
        //拿去user对象的用户名
        String userName=userinfo.getUsername();
        //根据用户名获取户主name，并查询户主对应的车位缴费数据
        Owner owner = ownerService.queryOwnerByName(userName);
        //获取ownerId
        carcharge.setOwnerId(owner.getId());
        PageInfo<Carcharge> pageInfo=carchargeService.queryCarchargeAll(page,limit,carcharge);

        return new JsonObject(0,"ok",pageInfo.getTotal(),pageInfo.getList());
    }
    @RequestMapping("/addCarcharge")
    @Transactional(propagation = Propagation.REQUIRED)
    public R addCarcharge(@RequestBody Carcharge carcharge){

        carchargeService.addCarcharge(carcharge);
        return R.ok();

    }
    @RequestMapping("/deleteByIds")
    public R deleteCarcharge(Integer[] ids){
        carchargeService.deleteCarcharge(ids);
        return R.ok();

    }
    @RequestMapping("/update")
    public R updateCarchare(Integer id){
        Carcharge carcharge = new Carcharge();
        carcharge.setId(id);
        carcharge.setStatus(1);
        carcharge.setPayDate(new Date());
        boolean flag=carchargeService.updateById(carcharge);
        if(flag){return R.ok();
        }else {
            return R.fail("缴费失败！");
        }
    }



}
